package com.ossean.match.main;
import org.apache.lucene.queryparser.classic.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.match.lucene.LuceneIndex;
import com.ossean.match.matchprocess.MatchIncrement;
import com.ossean.match.matchprocess.MemoToPrjMatch;
import com.ossean.match.matchprocess.NewPrjMonitor;
import com.ossean.match.matchprocess.PrjToMemoMatch;

@Component
public class Main {
	@Qualifier("newprjmonitor")
	@Autowired
	private NewPrjMonitor newPrjMonitor;
	
	@Qualifier("memotoprjmatch")
	@Autowired
	private MemoToPrjMatch memoToPrjMatch;
	
	@Qualifier("prjtomemomatch")
	@Autowired
	private PrjToMemoMatch prjToMemoMatch;
	
	@Qualifier("luceneindex")
	@Autowired
	private LuceneIndex luceneIndex;
	
	public static void main(String[] args) throws InterruptedException, ParseException {
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext_mybatis.xml");
		Main mainClass = applicationContext.getBean(Main.class);
		mainClass.start();
	}
	
	public void start() throws InterruptedException, ParseException {
		while (true) {
			MatchIncrement matchIncrement = new MatchIncrement();
			if (matchIncrement.getTaskToPrjId() != 0) {
				prjToMemoMatch.setMatchIncrement(matchIncrement);
				if (matchIncrement.getPrjHistory() == 0 && matchIncrement.getMemoHistory() == 0) {
					luceneIndex.run(matchIncrement);
				}
				newPrjMonitor.run();
				prjToMemoMatch.run();
			}
			else {
				newPrjMonitor.setMatchIncrement(matchIncrement);
				memoToPrjMatch.setMatchIncrement(matchIncrement);
				prjToMemoMatch.setMatchIncrement(matchIncrement);
				
				if (matchIncrement.getPrjHistory() == 0 && matchIncrement.getMemoHistory() == 0) {
					luceneIndex.run(matchIncrement);
				}
				
				newPrjMonitor.run();
				prjToMemoMatch.run();
				memoToPrjMatch.run();
			}
			
		    Thread.sleep(matchIncrement.getSleepTime());
		}
		
	}
}
